package uit.comstore.model.blo;

import java.util.List;

import org.hibernate.Session;

import uit.comstore.model.entity.Faq;
import uit.comstore.utils.HibernateSessionFactory;

public class FaqBLO {

	@SuppressWarnings("unchecked")
	public List<Faq> list() {
		List<Faq> faqs = null;
		Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		faqs = (List<Faq>) session.createQuery("from Faq").list();
		session.getTransaction().commit();
		return faqs;
	}
	public Faq delete(Long id){
		Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Faq faq = (Faq) session.load(Faq.class, id);
		if(null != faq) {
			session.delete(faq);
		}
		session.getTransaction().commit();
		return faq;
	}
	
	public Faq update(Faq f){
		Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Faq faq = (Faq)session.get(Faq.class, f.getId());
		faq.setDateAdded(f.getDateAdded());
		faq.setLastModified(f.getLastModified());
		faq.setStatus(f.getStatus());
		faq.setQuestion(f.getQuestion());
		faq.setAnswer(f.getAnswer());
		session.update(faq);
		session.getTransaction().commit();
		session.close();
		return faq;
	}
	public Faq add(Faq faq) {
		Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.save(faq);
		session.getTransaction().commit();
		return faq;
	}
	
}
